package com.wink_172.library.utils

import android.content.Context

/**
 * 暂时被MMKV替换
 * 保留代码 说不定以后会用到
 */
object SharedPreferencesTool {
    private const val SHARE_PREFERENCE_FILE_NAME = "test"

    //  http://blog.csdn.net/baidu_31093133/article/details/51476726
    fun getInt(context: Context, key: String?, defValue: Int): Int {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        return sharedPreferences.getInt(key, defValue)
    }

    fun setInt(context: Context, key: String?, value: Int) {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        sharedPreferences.edit().putInt(key, value).commit()
    }

    fun getFloat(context: Context, key: String?, defValue: Float): Float {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        return sharedPreferences.getFloat(key, defValue)
    }

    fun setFloat(context: Context, key: String?, value: Float) {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        sharedPreferences.edit().putFloat(key, value).commit()
    }

    fun getLong(context: Context, key: String?, defValue: Long?): Long {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        return sharedPreferences.getLong(key, 0)
    }

    fun setLong(context: Context, key: String?, value: Long) {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        sharedPreferences.edit().putLong(key, value).commit()
    }

    fun getString(context: Context, key: String?, defValue: String?): String? {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        return sharedPreferences.getString(key, defValue)
    }

    fun setString(context: Context, key: String?, value: String?) {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        sharedPreferences.edit().putString(key, value).commit()
    }

    fun getBoolean(context: Context, value: String?, defValue: Boolean?): Boolean {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        return sharedPreferences.getBoolean(value, defValue!!)
    }

    fun setBoolean(context: Context, key: String?, value: Boolean?) {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.putBoolean(key, value!!).commit()
    }

    fun remove(context: Context, key: String?) {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.remove(key).commit()
    }

    fun clear(context: Context) {
        val sharedPreferences =
            context.getSharedPreferences(SHARE_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.clear().commit()
    }
}